<!--
 * @Author: White
 * @description: 搜索结果
 * @LastEditTime: 2021-08-23 14:51:08
-->

<template>
    <div class="search">
        <div class="search-header">
            <h5>{{findtitle}}</h5>
            <span>{{findname}}</span>
        </div>
        <div class="search-tab">
            <el-tabs v-model="activeName" @tab-click="tabClick(activeName)">
                <el-tab-pane label="单曲" name="single">
                    <div class="single">
                        <div class="single-header">
                            <div class="single-title">音乐标题</div>
                            <div class="single-author">歌手</div>
                            <div class="single-album">专辑</div>
                            <div class="single-time">时长</div>
                        </div>
                        <div class="single-content">
                            <div class="single-item" v-for="(item, index) in single" :key="index" 
                            :class="{'active':index%2 != 0}"
                            @click="singlePlay(item.id)">
                                <div class="single-title">
                                    <span class="index">{{index + 1}}</span>
                                    <span class="name">{{item.name}}</span>
                                </div>
                                <div class="single-author">{{item.artists[0].name}}</div>
                                <div class="single-album">{{item.album.name}}</div>
                                <div class="single-time">{{item.duration}}</div>
                            </div>
                        </div>
                    </div>
                </el-tab-pane>
                <el-tab-pane label="歌手" name="singer">
                    <div class="singer">
                        <div class="singer-item" v-for="(item, index) in singer" :key="index">
                            <div class="singer-img">
                                <img :src="item.img1v1Url" alt="">
                            </div>
                            <div class="singer-text">
                                <span>{{item.name}}</span>
                            </div>
                        </div>
                    </div>
                </el-tab-pane>
                <el-tab-pane label="专辑" name="album">
                    <div class="album">
                        <div class="album-item" v-for="(item, index) in album" :key="index">
                            <div class="album-img">
                                <img :src="item.picUrl" alt="">
                                <span>{{item.name}}</span>
                                <span class="desc" v-if="item.alias[0]">({{item.alias[0]}})</span>
                            </div>
                            <div class="album-author">
                                <span>{{item.artist.name}}</span>
                            </div>
                        </div>
                    </div>
                </el-tab-pane>
                <el-tab-pane label="视频" name="video">
                    <div class="video">
                        <div class="video-item" v-for="(item, index) in video" :key="index">
                            <img :src="item.coverUrl" alt="">
                            <p class="video-title">{{item.title}}</p>
                            <p class="video-desc">{{item.creator[0].userName}}</p>
                        </div>
                    </div>
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>
</template>

<script>
import { reactive, onMounted, toRefs } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from "vuex"
import { getSearch, getMusicDetail } from '@/api/music'
export default {
    setup () {
        const store = useStore()
        const router = useRouter()
        const data = reactive({
            activeName: 'single',
            findtitle: '',
            findname: '',
            single: [],
            singer: [],
            album: [],
            video: []
        })
        const query = reactive({
            keywords: router.currentRoute.value.query.keywords,
            type: 1
        })
        onMounted(async ()=> {
            data.findtitle = router.currentRoute.value.query.keywords
            const res = await getSearch(query)
            if (res.data.code === 200) { 
                data.single = res.data.result.songs
                data.findname = `找到${res.data.result.songCount}首单曲`
            }
        })
        const tabClick = async(type) => {
            if (type == 'single') {
                query.type = 1
                const res = await getSearch(query)
                if (res.status === 200) {
                    data.findname = `找到${res.data.result.songCount}首单曲`
                }
            }
            if (type == 'singer') {
                query.type = 100
                const res = await getSearch(query)
                if (res.status === 200) {
                    data.singer = res.data.result.artists
                    data.findname = `找到首${res.data.result.artistCount}位歌手`
                }
            }
            if (type == 'album') {
                query.type = 10
                const res = await getSearch(query)
                if (res.data.code === 200) {
                    data.album = res.data.result.albums
                    data.findname = `找到${res.data.result.albumCount}张专辑`
                }
            }
            if (type == 'video') {
                query.type = 1014
                const res = await getSearch(query)
                if (res.data.code === 200) {
                    data.video = res.data.result.videos
                    data.findname = `找到${res.data.result.videoCount}个视频`
                }
            }
        }
        // 单曲播放
        const singlePlay = async(id) => {
            console.log(id)
            let parmas = { ids: id }
            const res = await getMusicDetail(parmas)
            store.commit("setCurrentSong", res.data)
        }
        return {
            ...toRefs(data),
            ...toRefs(query),
            tabClick,
            singlePlay
        }
    }
}
</script>

<style lang="scss" scoped>
.search {
    .search-header {
        display: flex;
        align-items: center;
        height: 60px;
        h5 {
            font-size: 20px;
            font-weight: 600;
            margin-right: 20px;
        }
        span {
            font-size:10px;
            font-weight: 300;
        }
    }
    // 选项框
    .search-tab {
        :deep(.el-tabs__item.is-active) {
            color: #c3473a;
        }
        :deep(.el-tabs__active-bar) {
            background-color: #c3473a;
        }
        :deep(.el-tabs__item:hover) {
            color: #c3473a;
        }
        :deep(.el-tabs__header) {
            margin: 0;
        }
        .single {
            .single-header {
            display: flex;
            font-size: 13px;
            color: #888;
            height: 38px;
            line-height: 38px;
                .single-title {
                    width: 50%;
                    text-align: center;
                }
                .single-author {
                    width: 15%;
                }
                .single-album {
                    width: 20%;
                }
                .single-time {
                    width: 15%;
                }
            }
            .single-content {
                color: #666;
                font-size: 12px;
                .active {
                    background-color: #fafafa;
                }
                .single-item {
                    display: flex;
                    flex-wrap: wrap;
                    align-items: center;
                    height: 30px;
                    line-height: 30px;
                    .single-title {
                        width: 50%;
                        text-align: left;
                        white-space: nowrap;  
                        text-overflow:ellipsis;
                        overflow:hidden;
                        .index {
                            display: inline-block;
                            padding: 0px 20px;
                            color: #d3d3d3;
                        }
                    }
                    .single-author {
                        width: 15%;
                        white-space: nowrap;  
                        text-overflow:ellipsis;
                        overflow:hidden;
                    }
                    .single-album {
                        width: 20%;
                        white-space: nowrap;  
                        text-overflow:ellipsis;
                        overflow:hidden;
                    }
                    .single-time {
                        width: 15%;
                    }
                }
            }
        }
        .singer {
            .singer-item {
                display: flex;
                align-items: center;
                height: 80px;
                .singer-img {
                    img {
                        width: 60px;
                        height: 60px;
                        border-radius: 10px;
                    }
                }
                .singer-text {
                    margin-left: 20px;
                    span {
                        font-size: 14px;
                        color: #6c9abc;
                        cursor: pointer;
                    }
                }
            }
        }
        .album {
            .album-item {
                height: 80px;
                display: flex;
                align-items: center;
                cursor: pointer;
                .album-img {
                    width: 50%;
                    display: flex;
                    align-items: center;
                    img {
                        width: 60px;
                        height: 60px;
                        border-radius: 10px;
                        margin-right: 20px;
                    }
                    span {
                        font-size: 14px;
                        color: #000;
                    }
                    .desc {
                        color: #888;
                        padding-left: 10px;
                    }
                }
                .album-author {
                    span {
                        font-size: 12px;
                        color: #6c9abc;
                    }
                }
            }
        }
        .video {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            margin-top: 20px;
            .video-item {
                width: 30%;
                margin-bottom: 10px;
                img {
                    width: 100%;
                    height: 150px;
                    border-radius: 10px;
                }
                .video-title {
                    font-size: 14px;
                    line-height: 30px;
                    color: #333;
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                }
                .video-desc {
                    font-size: 13px;
                    color: gray;
                }
            }
        }
    }
}
</style>